<?php
/*
 * 友情链接控制器
 * @author suosuo
 */
namespace Admin\Controller;
use Think\Controller;
class LinksController extends Controller {
	public function index () {
		$conds = array(
			'status'=>1,
		);
		$page = $_REQUEST['p'] ? $_REQUEST['p']:1;
		 $pageSize = $_REQUEST['pageSize'] ?$_REQUEST['pageSize'] : 5;
		 $links = D("Links")->getLinks($conds,$page,$pageSize);
		 $linksCount = D("Links")->getLinksCount($conds);
		 
		 $res = new \Think\Page($linksCount,$pageSize);
		 $pageRes = $res->show();
//		 print_r($pageRes);
		 $this->assign('pageRes',$pageRes);
		 $this->assign('links',$links);
		$this->display();
	}
	
	public function edit () {
		$Id = $_GET['id'];
		$links=D("Links")->find($Id);
		$this->assign('links',$links);
		$this->display();
	}
	
	public function save($data){
		$Id = $data['id'];
		unset($data['id']);
		try{
			$id=D("Links")->updataLinksById($Id,$data);
			if($id===false){
				return show(0, '更新失败');
			}
			return show(1, '更新成功');
		}catch(Exception $e){
			return show(0, $e->getMessage());
		}
	}
	
	public function add(){
		if($_POST){
			if(!isset($_POST['name']) || !$_POST['name'] ){
		 		return show(0, '站点名不可为空');
			}
			if(!isset($_POST['url']) || !$_POST['url']){
		 		return show(0, 'url不可为空');
			}
			
			if($_POST['id']){
				return $this->save($_POST);
			}
			
			$LinksId = D('Links')->insert($_POST);
			if($LinksId){
				return show(1,"新增成功");
			}else{
				return show(0,"新增失败");
			}
		}else{
			$this->display();
		}
	}
	
	public function del (){
		try{
			if($_POST){
				$id=$_POST['id'];
				$status=$_POST['status'];
				$res=D('Links')->updataStatusById($id,$status);
				if($res){
					return show(1, '操作成功！');
				}else{
					return show(0, '操作失败！');
				}
			}
		}catch(Exception $e){
			return show(0, $e->getMessage());
		}
		return show(0, '没有提交的数据');
	}
	
	
}